const hre = require("hardhat");

async function main() {
  console.log("Testing XLayerLaunchPad deployment with mock contracts...");
  
  // Deploy mock contracts first
  console.log("\n=== Deploying Mock Contracts ===");
  
  // Deploy Mock WETH
  const MockWETH = await hre.ethers.getContractFactory("MockWETH");
  const mockWETH = await MockWETH.deploy();
  await mockWETH.waitForDeployment();
  console.log("MockWETH deployed to:", await mockWETH.getAddress());
  
  // Deploy Mock Factory
  const MockFactory = await hre.ethers.getContractFactory("MockFactory");
  const mockFactory = await MockFactory.deploy();
  await mockFactory.waitForDeployment();
  console.log("MockFactory deployed to:", await mockFactory.getAddress());
  
  // Deploy Mock Router
  const MockRouter = await hre.ethers.getContractFactory("MockRouter");
  const mockRouter = await MockRouter.deploy(await mockWETH.getAddress(), await mockFactory.getAddress());
  await mockRouter.waitForDeployment();
  console.log("MockRouter deployed to:", await mockRouter.getAddress());
  
  console.log("\n=== Deploying XLayerLaunchPad ===");
  
  // Deploy XLayerLaunchPad with mock router
  const XLayerLaunchPad = await hre.ethers.getContractFactory("XLayerLaunchPad");
  const launchPad = await XLayerLaunchPad.deploy(await mockRouter.getAddress());
  await launchPad.waitForDeployment();
  
  console.log("XLayerLaunchPad deployed to:", await launchPad.getAddress());
  
  // Test basic functionality
  console.log("\n=== Testing Basic Functionality ===");
  
  // Check initial values
  const platformFee = await launchPad.platformFeePercent();
  const nextLaunchId = await launchPad.nextLaunchId();
  const owner = await launchPad.owner();
  const dexRouter = await launchPad.dexRouter();
  const dexFactory = await launchPad.dexFactory();
  
  console.log("Platform Fee:", platformFee.toString(), "basis points");
  console.log("Next Launch ID:", nextLaunchId.toString());
  console.log("Owner:", owner);
  console.log("DEX Router:", dexRouter);
  console.log("DEX Factory:", dexFactory);
  
  // Test contract constants
  const minTarget = await launchPad.MIN_TARGET();
  const maxTarget = await launchPad.MAX_TARGET();
  const maxFee = await launchPad.MAX_FEE();
  const launchDuration = await launchPad.LAUNCH_DURATION();
  
  console.log("\n=== Contract Constants ===");
  console.log("Min Target:", hre.ethers.formatEther(minTarget), "ETH");
  console.log("Max Target:", hre.ethers.formatEther(maxTarget), "ETH");
  console.log("Max Fee:", maxFee.toString(), "basis points");
  console.log("Launch Duration:", launchDuration.toString(), "seconds");
  
  console.log("\n✓ All contracts deployed and initialized successfully!");
  
  console.log("\n=== Deployment Summary ===");
  console.log("MockWETH:", await mockWETH.getAddress());
  console.log("MockFactory:", await mockFactory.getAddress());
  console.log("MockRouter:", await mockRouter.getAddress());
  console.log("XLayerLaunchPad:", await launchPad.getAddress());
}

main()
  .then(() => process.exit(0))
  .catch((error) => {
    console.error(error);
    process.exit(1);
  });